<template>
	<view class="container">
		<view class="margin-bottom-xs">
			<form>
				<view class="cu-form-group margin-top-xs">
					<view class="title text-black">手机号</view>
					<input v-model="fromParams.phone" placeholder="账户名" name="input" />
				</view>
				<!-- <view class="cu-form-group">
					<view class="title">验证码</view>
					<input v-model="fromParams.verifyCode" placeholder="请输入验证码" name="input" />
					<button v-if="countdown == 60" @click="getCode" class="cu-btn bg-green shadow">验证码</button>
					<button v-else class="cu-btn bg-gray shadow">{{ countdown }}秒</button>
				</view> -->
				<view class="cu-form-group">
					<view class="title text-black">旧密码</view>
					<input v-model="fromParams.oldPassword" placeholder="新密码" name="input" />
				</view>
				<view class="cu-form-group">
					<view class="title text-black">新密码</view>
					<input v-model="fromParams.newPassword" placeholder="新密码" name="input" />
				</view>
				<view class="padding-lr ">
					<view class="padding-sm flex justify-center">
						<button @click="handleLogin" style="width: 100%;"
							class="margin-left cu-btn lg round bg-green text-white text-size-28">立即修改</button>
					</view>
				</view>
			</form>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { ref } from 'vue';
	import { useSettingStore } from '@/stores/index';
	import { verifyCode, updatePwd } from '@/common/api/login';
	const fromParams = ref({
		phone: '',
		username: '',
		oldPassword: '',
		newPassword: ''
	});
	const setting = useSettingStore();
	const countdown = ref(60);
	const getCode = async (e : any) => {
		if (countdown.value != 60) {
			return;
		}
		const res : any = await verifyCode({ phone: fromParams.value.phone });
		if (res.code == 1000) {
			countdown.value = 60;
			let timer = setInterval(() => {
				if (countdown.value > 0) {
					countdown.value--;
				} else {
					clearInterval(timer);
				}
			}, 1000);
		}
	};
	const handleLogin = () => {
		updatePwd(fromParams.value).then((res : any) => {
			if (res.code == 1000) {
				setting.logout();
				uni.removeStorage({
					key: 'role',
					success() {
					}
				})
				setting.userInfo = {
					auth: 2,
					bindService: false,
					phone: "",
					avatar: "",
					city: "",
					country: "",
					gender: 0,
					language: "",
					realName: "游客",
					province: "",
					invitationCode: '',
					roleKeys: [],
					id: 0
				};
				uni.switchTab({
					url: '/pages/index/index'
				});
			} else {
				uni.showToast({
					title: res.msg
				})
			}
		});
	};
</script>

<style></style>